home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 104_01.zip / C1.C < prev    next >
Text File  |  1993-06-14  |  10KB  |  470 lines

  1. /*    >>>>> start ccl <<<<<<        */
  2. /*                    */
  3. /*    Compiler begins execution here  */
  4. /*                    */
  5. #ifndef TRUE    /*see if need to use include file */
  6. #include <c.def>
  7. #endif
  8. main()
  9.     {
  10.     glbptr=startglb;    /* clear global symbols */
  11.     locptr=startloc;    /* clear local symbols */
  12.     wqptr=wq;        /* clear while queue */
  13.     macptr=            /* clear the macro pool */
  14.     litptr=            /* clear literal pool */
  15.     nxtlab=         /* next label to assign */
  16.     sp =            /* stackptr (relative) */
  17.     errcnt=            /* no errors */
  18.     eof=            /* not eof yet */
  19.     input=            /* no input file */
  20.     input2=            /* or include file */
  21.     output=            /* no open units */
  22.     ncmp=            /* no open compound states */
  23.     ctext=            /* do not print text */
  24.     lastst=0;        /* no last statement yet */
  25.     cif=            /* state of #ifxxxx */
  26.     cmode=1;        /* enable preprocessing */
  27.     litlab=getlabel();    /* label number for literal poll */
  28.     /*                */
  29.     /*    compiler body        */
  30.     /*                */
  31.     ask();            /* get user options */
  32.     openout();        /* get an output file */
  33.     openin();        /* and initial input file */
  34.     header();        /* intro code */
  35.     parse();        /* process ALL input */
  36.     dumplits();        /* then dump literal pool */
  37.     dumpglbs();        /* and all static memory */
  38.     dumpextrn();        /* make all function defined extrn */
  39.     errorsummary();        /* summarize errors */
  40.     trailer();        /* follow-up code */
  41.     closeout();        /* close the output */
  42.     if (errcnt) unlink("a:$$$.sub"); /* if errors erase submit file */
  43.     return;            /* then exit to system */
  44.     }
  45. /*                    */
  46. /*    Process all input text        */
  47. /*                    */
  48. /* At this level, only static declarations,    */
  49. /*    defines, includes, and function        */
  50. /*    definitions are legal...        */
  51. parse()
  52. {
  53.     while (!eof) {    /* do until no more input */
  54.         if(amatch("char",4)) {
  55.             declglb(cchar);
  56.             ns();
  57.             }
  58.         else if(amatch("int",3)) {
  59.             declglb(cint);
  60.             ns();
  61.             }
  62.         else if(match("#asm")) doasm();
  63.         else if(match("#include")) doinclude();
  64.         else if(match("#define")) addmac(); 
  65.         else if(match("#ifndef")) doifndef();
  66.         else if(match("#ifdef")) doifdef(); 
  67.         else newfunc();
  68.         blanks();    /* force eof if pending */
  69.         }
  70.     }
  71. /*                    */
  72. /*    Dump the literal pool        */
  73. /*                    */
  74. dumplits()
  75. {
  76.     int j,k;
  77.  
  78.     if (litptr==0) return;  /* if nothing there, exit...*/
  79.     printlabel(litlab);
  80.     col();            /* print literal label */
  81.     k=0;             /* init an index... */
  82.     while (k<litptr) {     /*      to loop with */
  83.         defbyte();     /* pseudo-op to define byte */
  84.         j=10;       /* max bytes per line */
  85.         while(j--) {
  86.             outdec(litq[k++]);
  87.             if (j==0 || k>=litptr) {
  88.                 nl();      /* need <cr> */
  89.                 break;
  90.                 }
  91.             outbyte(',');   /* separate bytes */
  92.             }
  93.         }
  94.     litptr=0;
  95.     }
  96. /*                    */
  97. /*    Dump all static variables    */
  98. /*                    */
  99. dumpglbs()
  100. {
  101.     int j;
  102.     dsect("@globals");
  103.     cptr=startglb;
  104.     while(cptr<glbptr) {
  105.         if (cptr[ident] != function) {
  106.             /* do if anything but function */
  107.             outstr(cptr);col();
  108.                 /* output name as label... */
  109.             defstorage();   /*define storage */
  110.             j=((cptr[offset]&255)+
  111.                 ((cptr[offset+1]&255)<<8));
  112.                     /* calc # bytes */
  113.             outdec(j);      /* need that many */
  114.             nl();
  115.             }
  116.         cptr=cptr+symsiz;
  117.         }
  118.     }
  119. /*                    */
  120. /*    dump extrn function        */
  121. /*                    */
  122. dumpextrn()
  123. {
  124.     char *ptr;
  125.  
  126.     ptr=startglb;
  127.     while(ptr!=glbptr) {
  128.         if (!ptr[offset]) extrn(ptr);
  129.         ptr=ptr+symsiz;
  130.         }
  131.     }
  132. /*                    */
  133. /*    Report errors for user        */
  134. /*                    */
  135. errorsummary()
  136. {
  137.     /* see if anything left hanging... */
  138.     if (ncmp) error("missing closing bracket");
  139.         /* open compund statment ... */
  140.     if (!cif) error("missing closing #endif");
  141.     nl();
  142.     comment();
  143.     outdec(errcnt); /* total # errors */
  144.     outstr(" errors in compilation.");
  145.     nl();
  146.     }
  147. /*                    */
  148. /*    Get options from user        */
  149. /*                    */
  150. ask()
  151. {
  152.     int k;
  153.     kill();     /* clear input line */
  154.     pl("  * * *  small-c  version 1.1 compiler  * * *\n");
  155.     /* see if user wants to interleasve the c-text    */
  156.     /*      inform of comments (for clarity)     */
  157.     pl("Do you wish to c-text to appear? ");
  158.     gets(line);            /* get answer */
  159.     if(toupper(ch())=='Y') ctext=1;    /* user said yes */
  160.     }
  161. /*                    */
  162. /*    Get output filename        */
  163. /*                    */
  164. openout()
  165. {
  166.     kill();     /* erase line */
  167.     output=0;           /* start with none */
  168.     pl("Output filename? "); /* ask...*/
  169.     gets(line);     /* get a filename */
  170.     if(ch()==0)return;      /* none given... */
  171.     if ((output=fcreat(line,fout)) == ERROR) {  
  172.         output=0;      /* can't open */
  173.         error("Open failure");
  174.         }
  175.     kill();         /* erase line */
  176. }
  177. /*                    */
  178. /*    Get (next) input file        */
  179. /*                    */
  180. openin()
  181. {
  182.     input=0;        /* none to start with */
  183.     while (!input)        /* any above 1 allowed */
  184.         {kill();    /* clear line */
  185.         if(eof)break;    /* if user said none */
  186.         pl("Input filename? ");
  187.         gets(line);    /* get a name */
  188.         if(ch()==0) {
  189.             eof=1;
  190.             break;    /* none given... */
  191.             }
  192.         line1=0;
  193.         if ((input=fopen(line,finp)) == ERROR) {
  194.             input=0;    /* can't open it */
  195.             pl("Open failure");
  196.             }
  197.         }
  198.     kill();     /* erase line */
  199.     }
  200. /*                    */
  201. /*    Open an include file        */
  202. /*                    */
  203. doinclude()
  204. {
  205.     char fname[31];    /* place to open file name */
  206.     char fend;    /* ending character for file name */
  207.     int  lenght;    /* size of file name */
  208.     while(isspace(ch())) gch();     /* skip over to name */
  209.     fend=' ';
  210.     if (ch()=='<') {
  211.         fend='>';
  212.         gch();
  213.         }
  214.     if (ch()=='\"') {
  215.         fend='\"';
  216.         gch();
  217.         }
  218.     lenght=0;
  219.     while(lenght<30 && ch() && ch()!=fend) {
  220.         fname[lenght++]=ch();
  221.         gch();
  222.         }
  223.     fname[lenght]=0;
  224.     if (!(fend == ch() || (fend ==' ' &&  !ch()))) {
  225.         error("invaild file name for include");
  226.         kill();
  227.         return;
  228.         }
  229.     if (!lenght) {
  230.         error("missing file name for include");
  231.         kill();
  232.         return;
  233.         }
  234.     line2=0;
  235.     if (input2) {
  236.         error("nested include not aloud");
  237.         kill();
  238.         return;
  239.         }
  240.     if ((input2=fopen(fname,finp2)) == ERROR) { 
  241.         error("Open failure on include file");
  242.         input2=0;
  243.         }
  244.     kill();
  245.     }
  246. /*                    */
  247. /*    #ifndef             */
  248. /*                    */
  249. doifndef()
  250. {
  251.     char sname[namesize];
  252.     int lval[2];
  253.  
  254.     /* check to if define as constant */
  255.     blanks();
  256.     if (constant(lval)) {
  257.         cif=FALSE;
  258.         return;
  259.         }
  260.  
  261.     /* check to see if vaild sysmbol */
  262.     if (!symname(sname)) {
  263.         error("invaild identifier");
  264.         kill();
  265.         return;
  266.         }
  267.  
  268.     /* check to see if symbol is define */
  269.     if (findloc(sname) || findglb(sname))
  270.         cif=FALSE; else cif=TRUE;
  271.     }
  272. /*            */
  273. /*    #ifdef        */
  274. /*            */
  275. doifdef()
  276. {
  277.     char sname[namesize];
  278.     int  lval[2];
  279.  
  280.     /* check to see if constant */
  281.     blanks();
  282.     if (!constant(lval)) {
  283.         cif=FALSE;
  284.         return;
  285.         }
  286.  
  287.     /* check to see if vaild symbol    */
  288.     if (!symname(sname)) {
  289.         error("invaild identifier");
  290.         kill();
  291.         }
  292.  
  293.     /* check to see if symbol is define */
  294.     if (findloc(sname) || findglb(sname)) 
  295.         cif=TRUE; else cif=FALSE;
  296.     }
  297. /*                    */
  298. /*    Close the output file        */
  299. /*                    */
  300. closeout()
  301. {
  302.     if (output) {    /* if open mark end, close it */
  303.         putc(26,fout);
  304.         fflush(fout);
  305.         fclose(output);
  306.         }
  307.     output=0;          /* mark as closed */
  308.     }
  309. /*                    */
  310. /*    Declare a static variable    */
  311. /*    (i.e. define for use)        */
  312. /*                    */
  313. /* makes an entry in the symbol table so subsequent */
  314. /*  references can call symbol by name  */
  315. declglb(typ)        /* typ is cchar or cint */
  316.     int typ;
  317. {
  318.     int j;
  319.     int count;
  320.     char sname[namesize];
  321.  
  322.     while(1) {
  323.         count=0;    
  324.         if (endst()) return;
  325.         if(match("*")) {
  326.             j=pointer;
  327.             ++count; 
  328.             while(ch()=='*') {
  329.                 ++count;
  330.                 gch();
  331.                 }
  332.             }        
  333.         else j=variable;
  334.         if (!symname(sname)) illname();
  335.         if(findglb(sname)) multidef(sname);
  336.         data_parse(sname,typ,statik,j,count);
  337.         if (match(",")==0) return; /* more? */
  338.         }
  339.     }
  340. /*                    */
  341. /*      Declare local variables        */
  342. /*      (i.e. define for use)        */
  343. /*                    */
  344. /* works just like "declglb" but modifies machine stack */
  345. /*    and adds symbol table entry with appropriate    */
  346. /*    stack offset to find it again            */
  347. declloc(typ)        /* typ is cchar or cint */
  348. int typ;
  349. {
  350.     int j;
  351.     int count;
  352.     char sname[namesize];
  353.     while(1) {
  354.         count=0;
  355.         if (endst()) return;
  356.         if (match("*")) { 
  357.             j=pointer;
  358.             ++count;
  359.             while(ch()=='*') {
  360.                 ++count;
  361.                 gch();
  362.                 }
  363.             }
  364.         else j=variable;
  365.         if (!symname(sname)) illname();
  366.         if (findloc(sname)) multidef(sname);
  367.         data_parse(sname,typ,stkloc,j,count);
  368.         if (!match(",")) return;
  369.         }
  370.     }
  371. /*                        */
  372. /*    parse out data item and add to symbol    */
  373. /*    symbol table.                */
  374. /*                        */
  375. /*    written April 8, 1981 By Mike Bernson    */
  376. /*                        */
  377. data_parse(sname,typ,class_id,ident_typ,count)
  378. char *sname;        /* pointer to name of symbol */
  379. char typ;        /* type of data char or int */
  380. char class_id;        /* typ of storage static automic */
  381. char ident_typ;        /* type of varble pointer or varble */
  382. int  count;        /* number of level of pointers */
  383. {
  384.     int size;    /* amount of memory needed for varble */
  385.     int value;    /* used to hold value for offset */
  386.     char *ptr;    /* pointer to free slot in symbol table */
  387.  
  388.     /* check to see if subscript */
  389.     if (match("[")) {
  390.  
  391.         /* add 1 to inderict count */
  392.         ++count;
  393.  
  394.         /* check to see null subscript */
  395.         if (size=needsub()) {
  396.  
  397.             /* subscript size not null */
  398.             if (ident_typ==pointer) {
  399.  
  400.                 /* array of pointer *temp[size] */
  401.                 size=size+size;
  402.                 }
  403.             else {
  404.  
  405.                 /* array temp[size] */
  406.                 ident_typ=array;
  407.                 if (typ==cint) size=size+size;
  408.                 }
  409.             }
  410.         else {
  411.  
  412.             /* null size subscript */
  413.             ident_typ=pointer;
  414.             size=2;
  415.             }
  416.         }
  417.     else {
  418.         /* no subscript */
  419.         if (ident_typ==pointer) size=2;
  420.         else if (typ == cint) size=2; else size=1;
  421.         }
  422.     /* add data element to symbol table */
  423.     switch(class_id) {
  424.  
  425.         /* global symbol */
  426.         case statik :
  427.             if (glbptr>=endglb) {
  428.                 error("global symnol table overflow");
  429.                 return 0;
  430.                 }
  431.             ptr=glbptr;
  432.             glbptr+=symsiz;
  433.             value=size;
  434.             break;
  435.     
  436.         /* stack local varble */ 
  437.         case stkloc :
  438.             sp=modstk(sp-size);
  439.             value=sp;
  440.             if (locptr>=endloc) {
  441.                 error("local symbol table overflow");
  442.                 return 0;
  443.                 }
  444.             ptr=locptr;
  445.             locptr+=symsiz;
  446.             break;
  447.  
  448.         /* arg stack */
  449.         case stkarg :
  450.             value=argstk;
  451.             argstk-=2;
  452.             if (locptr>=endloc) {
  453.                 error("local symbol table overflow");
  454.                 return 0;
  455.                 }
  456.             ptr=locptr;
  457.             locptr+=symsiz;
  458.             break;
  459.         }
  460.     /* file in symbol table entry */
  461.     strcpy(ptr+name,sname);
  462.     ptr[ident]=ident_typ;
  463.     ptr[type]=typ;
  464.     ptr[storage]=class_id;
  465.     ptr[offset]=value;
  466.     ptr[offset+1]=value>>8;
  467.     ptr[indcnt]=count;
  468.     return TRUE;
  469.     }
  470.     /* a